package com.hxx.widget.sheet

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.RadioButton
import androidx.annotation.DrawableRes
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.hxx.widget.R
import com.hxx.widget.switch.SwitchButton

class BottomSheetDialog(val context: Context, private val option: Option = Option()) {
    private val items: ArrayList<BottomSheetItem> = arrayListOf()
    fun show(items: List<BottomSheetItem>) {
        this.items.addAll(items)
        showDialog()
    }

    fun show(vararg items: BottomSheetItem) {
        this.items.addAll(items)
        showDialog()
    }

    private fun showDialog() {
        val bottomSheetDialog = BottomSheetDialog(context, R.style.widget_NoTitleDialog)
        val inflate = LayoutInflater.from(context)
        val view = inflate.inflate(R.layout.widget_layout_bottom_sheet_dialog_container, null)

        val containerView = view.findViewById<LinearLayout>(R.id.widget_bottom_sheet_container)
        val confirmButton = view.findViewById<View>(R.id.widget_bottom_sheet_confirm)
        val cancelButton = view.findViewById<View>(R.id.widget_bottom_sheet_cancel)
        val footView = view.findViewById<View>(R.id.widget_bottom_sheet_foot)
        val titleView = view.findViewById<AppCompatTextView>(R.id.widget_bottom_sheet_title)

        if (!option.cancelShow) {
            cancelButton.visibility = View.GONE
        } else {
            cancelButton.setOnClickListener {
                bottomSheetDialog.dismiss()
            }
        }
        if (!option.confirmShow) {
            confirmButton.visibility = View.GONE
        } else {
            confirmButton.setOnClickListener {
                bottomSheetDialog.dismiss()
            }
        }
        if (!option.confirmShow && !option.cancelShow) {
            footView.visibility = View.GONE
        }

        if (option.title.isNullOrBlank()) {
            titleView.visibility = View.GONE
        } else {
            titleView.text = option.title
        }

        items.forEach {
            val itemView = createItem(it)
            containerView.addView(itemView)
        }

        bottomSheetDialog.setContentView(view)
        bottomSheetDialog.setCanceledOnTouchOutside(true)
        bottomSheetDialog.setCancelable(true)
        // bottomSheetDialog.window?.setDimAmount(0f) //取消背景暗色蒙版
        bottomSheetDialog.dismissWithAnimation = true
        val mDialogBehavior = BottomSheetBehavior.from(view.parent as View)

        mDialogBehavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
            override fun onStateChanged(view: View, i: Int) {
            }

            override fun onSlide(view: View, v: Float) {

            }
        })
        bottomSheetDialog.show()
    }

    private fun createItem(item: BottomSheetItem): View {
        val itemView = BottomSheetListItem(context)
        itemView.setTitleIcon(item.title, item.icon)
        when (item.component) {
            is Arrow -> {
                val arrowImageView = AppCompatImageView(context)
                arrowImageView.setPadding(50, 0, 0, 0)
                arrowImageView.setImageResource(R.drawable.widget_ic_bx_arrow_right)
                itemView.replaceChildView(arrowImageView)
                item.component.applyHotView(itemView,item)
            }
            is Switch -> {
                val switchView = SwitchButton(context)
                switchView.setPadding(10,0,30,0)
                itemView.replaceChildView(switchView)
                item.component.applyHotView(switchView,item)
            }
            is Radio -> {
                val radioView = RadioButton(context)
                itemView.replaceChildView(radioView)
                item.component.applyHotView(radioView,item)
            }
            is Null -> {
                item.component.applyHotView(itemView,item)
            }
        }
        return itemView
    }

    class Option {
        var title: String? = null
        var confirmShow = false
        var cancelShow = false
    }


}
data class BottomSheetItem(
    val title: String,
    @DrawableRes val icon: Int,
    val component: ComponentWidget<*>
)
interface ComponentWidget<T : View> {
    fun applyHotView(view: T,item:BottomSheetItem)
}

/**
 * 箭头
 */
abstract class Arrow : ComponentWidget<View>

/**
 * 选择
 */
abstract class Switch : ComponentWidget<SwitchButton>

/**
 * 单选
 */
abstract class Radio : ComponentWidget<RadioButton>

/***
 * 默认啥都不是
 */
abstract class Null : ComponentWidget<View>